package com.company.ljh.medium;
/**
 * 1679
 */

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class 和数对的最大数目K {
    public int maxOperations(int[] nums, int k) {
        HashMap<Integer,Integer> map = new HashMap();
        for(int num:nums){
            Integer size = map.get(num);
            if(size != null){
                map.put(num,size+1);
            }else{
                map.put(num,1);
            }
        }
        int times = 0;
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        while(iterator.hasNext()){
            Map.Entry<Integer, Integer> entry = iterator.next();
            int num = entry.getKey();
            int size = entry.getValue();
            Integer next = map.get(k - num);
            if(next != null){
                if(k - num == num){
                    times+=Math.min(size,next)/2;
                }
                else
                    times+=Math.min(size,next);
                iterator.remove();
            }
        }
        return times;

    }
}
